package cn.frank.framework.king.conf.mvc;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * @author frank
 * @ClassName: springMVC配置 ConfigForWebMvc
 * @Description: 注意: 不要继承extends WebMvcConfigurationSupport, boot项目只能生效一个,这样是有缺陷的
 * @email renjiangyang@163.com
 * @date 2021年4月27日 上午11:06:23
 * @Copyright: 2020 renjiangyang  All rights reserved.
 */
@Configuration
@Slf4j
@Data
public class WebMvcConfig implements WebMvcConfigurer {

    public WebMvcConfig() {
        super();
        // log.info("\033[34m\033[0m");
        log.info("\033[34m [kingm3u8] loading WebAutoMvcConfig \033[0m");
    }

    /**
     * 资源文件 配置
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        for (WebMvcResourceEnum enumConstant : WebMvcResourceEnum.values()) {
            // enumConstant.clean(true);
            enumConstant.init();
        }

        // 1. 通用静态资源路径（支持打包在 classpath 中，或运行目录 resources 下的文件夹）
        List<String> defaultResources = new ArrayList<>();
        defaultResources.add("classpath:/resources/");
        defaultResources.add("classpath:/static/");
        defaultResources.add("classpath:/public/");
        defaultResources.add("classpath:/key/");
        defaultResources.add("file:./resources/public/");
        defaultResources.add("file:./resources/static/");

        // 2. 映射所有静态资源到 /**，例如 index.html、js、css 等
        ResourceHandlerRegistration defaultRegistry = registry.addResourceHandler("/**");
        defaultRegistry.addResourceLocations(defaultResources.toArray(new String[0]));
        defaultResources.forEach(resource ->
                log.info("\033[34m      |-- resource={}\033[0m", resource)
        );

        // 3. 映射上传文件路径 /upload/**，指向本地文件夹 ./upload/
        List<String> uploadResources = new ArrayList<>();
        uploadResources.add("file:./upload/"); // 真实路径
        ResourceHandlerRegistration uploadRegistry = registry.addResourceHandler("/upload/**");
        uploadRegistry.addResourceLocations(uploadResources.toArray(new String[0]));
        uploadResources.forEach(resource ->
                log.info("\033[34m      |-- upload Resource={}\033[0m", resource)
        );

        // 4. 映射 Swagger 静态资源（访问 doc.html 和 webjars）
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * 资源路径转发
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // registry.addViewController("/").setViewName("index.html");
        registry.addViewController("/").setViewName("forward:/index.html");
        registry.addViewController("/frank").setViewName("redirect:doc.html");
    }

}
